//
//  DayAndNightViewController.m
//  BobNews
//
//  Created by lanou3g on 16/5/17.
//  Copyright © 2016年 liu. All rights reserved.
//

#import "DayAndNightViewController.h"
#import "TouCollectionTableController.h"
#import "AppDelegate.h"
#import "LoginViewController.h"
@interface DayAndNightViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *headImgView;

@property (weak, nonatomic) IBOutlet UISwitch *dayAndNightSwitch;
@property(nonatomic,strong)UIView *darkView;
@end

@implementation DayAndNightViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    self.title = @"设置";
    
    [self.dayAndNightSwitch addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];
//    self.dayAndNightSwitch.on = no;
    self.headImgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headImgViewAction:)];
    [self.headImgView addGestureRecognizer:tap];
    self.headImgView.layer.cornerRadius = 50;
    self.headImgView.layer.masksToBounds = YES;
    
}
- (void)headImgViewAction:(UITapGestureRecognizer *)tap {
    UIImagePickerController *imgPick = [[UIImagePickerController alloc]init];
    imgPick.delegate = self;
    imgPick.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:imgPick animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    self.headImgView.image = image;
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)didClickLoginBtn:(UIButton *)sender {
    
    LoginViewController *loginVC = [[LoginViewController alloc]init];
    loginVC.title = @"登录";
    [self.navigationController pushViewController:loginVC animated:YES];
}
- (IBAction)didClickCollectionBtn:(UIButton *)sender {
    TouCollectionTableController *collectionTVC = [[TouCollectionTableController alloc]init];
    UINavigationController *collectionNavc = [[UINavigationController alloc]initWithRootViewController:collectionTVC];
    [self presentViewController:collectionNavc animated:YES completion:^{
        
    }];
}

- (void)changeValue:(UISwitch *)swi {
    if (swi.on) {
        //添加半透明view到window上
        UIApplication *app = [UIApplication sharedApplication];
        AppDelegate *delegate = (AppDelegate *)app.delegate;
        _darkView = [[UIView alloc]initWithFrame:self.view.frame];
        //设置view的背景色
        _darkView.backgroundColor = [UIColor blackColor];
        _darkView.alpha = 0.3;
        //关闭view的用户交互(响应者链)
        _darkView.userInteractionEnabled = NO;
        [delegate.window addSubview:_darkView];
        
    } else {
        [_darkView removeFromSuperview];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
